<template>
	<view class="data-list">
		<view class="head">
			<view class="item" v-for="(item,index) in navList" :key="index" :class="{active:index === activeNum}"
				@click="handleActiveNum(index,item)">
				{{item.name}}
				<view class="line"></view>
			</view>
		</view>
		<uni-popup ref="popup" type="top" class="popup" :padding="0">
			<view class="chunk" :style="{height: titleHeight + 'px'}"></view>
			<view class="item" v-for="(item,index) in popupList" :key="index" :class="{active:popupIndex === index}"
				@click="handlePopupIndex(index)">
				{{item}}
			</view>
		</uni-popup>

		<view class="select">
			<view class="item collect" @click="handleSelect(0)">
				<text class="text" :class="{active: selectNum === 0}">综合</text>
				<image v-if="selectNum === 0" class="subscript" src="@/static/index/subscript-active.png"></image>
				<image v-else class="subscript" src="@/static/index/subscript.png"></image>
			</view>
			<view class="item price" @click="handleSelect(1)">
				<text class="text" :class="{active: selectNum === 1}">价格</text>
				<image v-if="selectNum === 1" class="subscript" src="@/static/index/subscript-active.png"></image>
				<image v-else class="subscript" src="@/static/index/subscript.png"></image>
			</view>
			<view class="item screen">
				<text class="text">筛选</text>
				<image class="icon-screen" src="@/static/index/screen.png"></image>
			</view>
		</view>
		<view class="waterfall">
			<view class="item" v-for="(item) in dataList" :key="item.goods_id" @click="handleGoodsDetail(item)">
				<image v-if="item.new_grade == 95" class="new" src="@/static/index/95.png"></image>
				<image v-else-if="item.new_grade == 99" class="new" src="@/static/index/99.png"></image>
				<image v-else class="new" src="@/static/index/new.png"></image>
				<image class="img" :src="getImg(item.imgurl)" :lazy-load="true" mode="widthFix"></image>
				<view class="activity" :class="{active:!item.activity_title}">
					<view class="line">
						<view class="daily">限时活动</view>
						<view class="discount">
							{{item.activity_title}}
						</view>
					</view>
					<view class="current">
						<view class="current-title">卷后到手价</view>
						<view class="current-price">
							<view class="current-icon">￥</view>
							<view class="current-value">{{item.dayRent_new}}</view>
						</view>
					</view>
				</view>
				<view class="title">
					{{item.extension_title}}
				</view>
				<view class="attribute">
					<view class="i" :class="ele.color" v-for="(ele,index) in getLabel(item.rent_label)" :key="index">
						{{ele.title}}
					</view>
				</view>
				<view class="now-price">
					<text class="icon">￥</text>
					<text class="yuan">{{getPrice(item.min_price,0)}}.</text>
					<text class="corner">{{getPrice(item.min_price,1)}}/天</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "dataList",
		props: {
			titleHeight: {
				type: Number,
				default: 0,
			},
		},
		data() {
			return {
				activeNum: 0,
				selectNum: 0,
				dataList: [],
				popupIndex: 0,
				popupList: ["默认排序", "降价排序", "价格升序", "最新上架"],
				navList: [],
				params: {
					page: 1,
					cateId: 1,
					orderby_name: "id",
					sort: "desc",
				},
				isLoading: false,
				noMoreData: false,
			}
		},
		mounted() {
			this.getData();
			this.getCateList();
		},
		methods: {
			handleGoodsDetail(item) {
				uni.navigateTo({
					url: `/pagesA/productDetails/productDetails?goods_id=${ item.goods_id }`
				})
			},
			getLabel(string) {
				const labelData = [];
				string.split(",").forEach(item => {
					const obj = {
						title: item,
					}
					if (item === "包邮") {
						obj.color = "green"
					} else {
						obj.color = "red"
					}
					labelData.push(obj);
				})
				return labelData;
			},
			getImg(url) {
				return this.$imgURL2 + url;
			},
			getCateList() {
				uni.request({
					url: "https://txxzdata.tingxiangzu.com/Alipay/Index/categoryList",
					success: (res) => {
						this.navList = res.data.data;
					}
				})
			},
			handlePopupIndex(index) {
				this.popupIndex = index;
				switch (index) {
					case 0:
						this.params.orderby_name = "id";
						this.params.sort = "desc";
						break;
					case 1:
						this.params.orderby_name = "min_price";
						this.params.sort = "desc";
						break;
					case 2:
						this.params.orderby_name = "min_price";
						this.params.sort = "asc";
						break;
					case 3:
						this.params.orderby_name = "update_time";
						this.params.sort = "desc";
						break;
				}
				this.resetData();
			},
			getData() {
				if (this.isLoading || this.noMoreData) return;
				this.isLoading = true;
				uni.request({
					url: this.$apiUrl + "Index/indexList",
					data: this.params,
					success: (res) => {
						const data = res.data.data.data;
						this.dataList.push(...data);
						if (res.data.data.data.length) {
							this.params.page++;
						} else {
							this.noMoreData = true;
						}
					},
					complete: () => {
						this.isLoading = false;
					}
				})
			},
			handleSelect(index) {
				this.selectNum = index;
				if (index === 0 || index === 1) {
					this.$refs.popup.open();
				}
			},
			handleActiveNum(index, item) {
				this.activeNum = index;

				/*  */
				this.params.cateId = item.id;
				this.resetData();
			},
			resetData() {
				this.params.page = 1;
				this.noMoreData = false;
				this.dataList.splice(0);
				this.getData();
			},
			getPrice(price, index) {
				const priceArray = String(price).split(".");
				return priceArray[index];
			},
		}
	}
</script>

<style lang="scss" scoped>
	.data-list {
		margin-top: 24rpx;

		.head {
			display: flex;
			justify-content: space-between;

			.item {
				display: flex;
				flex-direction: column;
				align-items: center;
				font-size: 32rpx;
				color: #767676;

				&.active {
					color: #454545;
					font-weight: bold;

					.line {
						background-image: linear-gradient(to right, #E43C3A, #F9615F);
					}
				}

				.line {
					width: 70%;
					height: 10.5rpx;
					border-radius: 5rpx;
					margin-top: 8rpx;
				}
			}


		}

		.select {
			display: flex;
			margin: 24rpx 0;
			align-items: center;
			height: 30rpx;
			justify-content: space-between;

			.item {
				font-size: 28rpx;
				display: flex;
				align-items: center;

				.text {
					&.active {
						color: #E43C3A;
					}
				}

				.subscript {
					width: 18rpx;
					height: 14rpx;
					margin-left: 8rpx;
				}

				.icon-screen {
					width: 18.7rpx;
					height: 22rpx;
					margin-left: 8rpx;
				}
			}

			.screen {
				padding-left: 58rpx;
				border-left: 2px solid #D8D8D8;
			}
		}

		.popup {
			padding: 0 !important;

			.item {
				background-color: #f9f9f9;
				// padding-bottom: 40rpx;
				padding: 24rpx;
				font-size: 28rpx;
				font-weight: bold;

				&.active {
					color: #ff4445;
				}

				&:last-of-type {
					padding-bottom: 100rpx;
				}
			}
		}

		.waterfall {
			display: flex;
			flex-wrap: wrap;

			.item {
				width: 330rpx;
				// height: 532rpx;
				border-radius: 16rpx;
				box-sizing: border-box;
				position: relative;
				background-color: white;
				padding: 20rpx 24rpx;
				box-shadow: 0rpx 2rpx 10rpx 0rpx rgba(0, 0, 0, 0.1);
				overflow: hidden;
				margin-bottom: 32rpx;

				&:nth-of-type(odd) {
					margin-right: 42rpx;
				}

				.big-tag {
					width: 278rpx;
					height: 72rpx;
				}

				.new {
					position: absolute;
					top: 0;
					left: -5rpx;
					width: 84.85rpx;
					height: 86.37rpx;
					z-index: 10;
				}

				.img {
					width: 216rpx;
					margin-left: 50%;
					transform: translateX(-50%);
				}

				.activity {
					height: 50rpx;
					position: relative;

					&.active {
						opacity: 0;
						z-index: -9999;
					}

					.line {
						background-color: #DB211F;
						border-radius: 10rpx;
						height: 45rpx;
						display: flex;
						align-items: center;

						.daily {
							width: 80rpx;
							height: 45rpx;
							border-radius: 10rpx;
							background-color: #7600ED;
							color: white;
							display: flex;
							align-items: center;
							justify-content: center;
							font-size: 16rpx;
						}

						.discount {
							font-size: 16rpx;
							color: #FEFFED;
							margin-left: 8rpx;
						}
					}

					.current {
						position: absolute;
						width: 120rpx;
						height: 72rpx;
						bottom: 2rpx;
						right: -6rpx;
						background-image: url("@/static/index/cuurent-price.png");
						background-repeat: no-repeat;
						background-size: cover;
						color: white;
						padding-left: 36rpx;
						box-sizing: border-box;
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;

						.current-title {
							font-size: 10rpx;
							line-height: 1;
							margin-top: 5rpx;
							// transform: scale(.8);
						}

						.current-price {
							display: flex;
							align-items: baseline;
							margin-top: 5rpx;

							.current-icon {
								font-size: 12rpx;
								// transform: scale(.8);
							}

							.current-value {
								line-height: 1;
								font-size: 24rpx;
							}
						}
					}
				}

				.title {
					font-size: 24rpx;
					-webkit-line-clamp: 2;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					overflow: hidden;
				}

				.attribute {
					margin-top: 8rpx;
					font-size: 20rpx;
					display: flex;
					flex-wrap: wrap;

					.i {
						border-radius: 8rpx;
						height: 32rpx;
						margin-right: 8rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						padding: 0rpx 5rpx;
						margin-bottom: 5px;
					}

					.red {
						color: #E43C3A;
						border: 1px solid #E43C3A;
					}

					.green {
						color: #1DB03C;
						border: 1px solid #1DB03C;
					}
				}

				.now-price {
					color: #E43C3A;
					white-space: nowrap;

					.icon {
						font-size: 16rpx;
					}

					.yuan {
						font-size: 28rpx;
					}

					.corner {
						font-size: 16rpx;
					}
				}
			}
		}
	}
</style>
